package br.com.midiaseducativas.tutor.jogos.associativegame {
	import br.com.midiaseducativas.tutor.jogos.associativegame.events.CardEvent;
	import br.com.midiaseducativas.tutor.jogos.associativegame.vo.CardVO;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.net.URLRequest;
	
	/**
	 * Owner: Caio
	 * Date: Apr 18, 2011
	 */
	
	
	public class Card extends EventDispatcher{
		
		private var _vo:CardVO;
		private var _visual:CardSWC;
		
		private var loader:Loader;
		
		public function Card() {
			super();
			this.loader = new Loader();
			this._visual = new CardSWC();
			this._visual.addEventListener(MouseEvent.CLICK, onClick);
		}

		public function get visual():CardSWC {
			return _visual;
		}

		

		public function get vo():CardVO
		{
			return _vo;
		}

		public function set vo(value:CardVO):void
		{
			_vo = value;
			if (this.vo.type == "img") {
				this.loader.load(new URLRequest(this.vo.source));
				//trace(this.vo.source);
				this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			}else {
				this._visual.cardText.text = this.vo.source;
			}
		}
		
		protected function onLoadComplete(event:Event):void {
			
			//this._visual.cardText.text = this.vo.source;
			//this._visual.shape.visible = false;
			//this._visual.addChild(this._visual.cardText);
			if (this.loader.height > this.loader.width) {
				this.loader.height = this._visual.height * 0.9;
				this.loader.scaleX = this.loader.scaleY;
			} else {
				this.loader.width = this._visual.width * 0.9;
				this.loader.scaleY = this.loader.scaleX;
			}
			
			this.visual.addChild(this.loader);
			this.loader.content.cacheAsBitmap = true;
			Bitmap(this.loader.content).smoothing = true;
			
			this.loader.x = -(this.loader.width)/2;
			this.loader.y = -(this.loader.height)/2;
		}
		
		protected function onClick(event:MouseEvent):void {
			this._visual.shape.filters = [new GlowFilter(0x999999, 1, 50, 50)];
			this.dispatchEvent(new CardEvent(CardEvent.CLICK, this));
		}
		
		public function removeClickEventListener():void {
			this._visual.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		
	}
}